package com.tangpian.sna.core.analysis.svm.pre;

import java.io.File;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import edu.udo.cs.wvtool.main.WVTDocumentInfo;
import edu.udo.cs.wvtool.main.WVTInputList;
import edu.udo.cs.wvtool.util.WVToolLogger;

public class LineInputList implements WVTInputList {

	/** the list of input files */
	private List inputList = null;

	/** the number of classes */
	private int numClasses;

	/**
	 * Creates a new empty instance of WVTInputList
	 * 
	 * @param numClasses
	 *            the number of class values, 0 if no classes are provided
	 */
	public LineInputList(int numClasses) {

		inputList = new ArrayList();
		this.numClasses = numClasses;
	}


	/**
	 * Return the list of entries
	 * 
	 * @param expanded
	 *            should the list be expanded? (directoriy entries are converted
	 *            to a list of files)
	 * @return enumeration of the lists entries
	 */
	public Iterator getEntries(boolean expanded) {

		if (!expanded)
			return inputList.iterator();

		else {
			Iterator listElements = inputList.iterator();
			List expandedElements = new ArrayList();

			// Go through all entries and check, whether they point to
			// directories
			while (listElements.hasNext()) {
				WVTDocumentInfo docInfo = (WVTDocumentInfo) listElements.next();
				File inputFile = new File(docInfo.getSourceName());

				if (inputFile.exists()) {
					if (inputFile.isDirectory()) {
						// If a directory is found get all files contained in it
						// and add them to the expanded list
						File fileList[] = inputFile.listFiles();
						for (int i = 0; i < fileList.length; i++) {
							// When creating a new entry, preserve all
							// information but the
							// location of the file
							if (fileList[i].isFile()) {
								if (docInfo.hasClassValue()) {
									expandedElements.add(new WVTDocumentInfo(
											fileList[i].getAbsolutePath(),
											docInfo.getContentType(), docInfo
													.getContentEncoding(),
											docInfo.getContentLanguage(),
											docInfo.getClassValue()));
								} else {
									expandedElements.add(new WVTDocumentInfo(
											fileList[i].getAbsolutePath(),
											docInfo.getContentType(), docInfo
													.getContentEncoding(),
											docInfo.getContentLanguage()));
								}
							}
						}
					} else {
						expandedElements.add(docInfo);
					}
				} else {
					WVToolLogger
							.getGlobalLogger()
							.logMessage(
									"File "
											+ inputFile.getAbsolutePath()
											+ " not found. Assuming the text is directly encoded as document source...",
									WVToolLogger.WARNING);
					expandedElements.add(docInfo);
				}
			}

			return expandedElements.iterator();
		}

	}

	/**
	 * Add an entry to the list.
	 * 
	 * @param d
	 *            the entry
	 */
	public void addEntry(WVTDocumentInfo d) {

		inputList.add(d);

	}

	/**
	 * Remove an entry from the list.
	 * 
	 * @param d
	 *            the entry
	 */
	public void removeEntry(WVTDocumentInfo d) {

		inputList.remove(d);

	}

	/**
	 * Returns the numClasses.
	 * 
	 * @return int
	 */
	public int getNumClasses() {
		return numClasses;
	}

	public Iterator getEntries() {

		return getEntries(true);
	}

}
